package cn.jit.ssm.service.impl;

import cn.jit.ssm.beans.entity.JitRole;
import cn.jit.ssm.beans.entity.JitRoleModule;
import cn.jit.ssm.constant.MethodType;
import cn.jit.ssm.mapper.JitRoleModuleMapper;
import cn.jit.ssm.service.ModuleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Cecurio
 * @create: 2017-12-17 20:49
 **/
@Service
public class ModuleServiceImpl implements ModuleService {
    @Autowired
    private JitRoleModuleMapper jitRoleModuleMapper;

    @Override
    public boolean secureValid(String userName, String[] code, MethodType type) {
        List<JitRoleModule> roleModuleList = jitRoleModuleMapper.selectByUserName(code,userName);

        if (CollectionUtils.isEmpty(roleModuleList)) {
            return false;
        }

        for (JitRoleModule te : roleModuleList) {
            if (type == MethodType.FIND && te.getFinds())
                return true;
            else if (type == MethodType.ADD && te.getAdds())
                return true;
            else if (type == MethodType.MODIFY && te.getModifys())
                return true;
            else if (type == MethodType.DELETE && te.getDeletes())
                return true;
        }

        return false;
    }
}
